﻿#ifndef HTTPPOSTREQUEST
#define HTTPPOSTREQUEST

#include <QNetworkAccessManager>    // 包含Qt网络访问管理器类的定义，用于处理网络请求
#include <QNetworkRequest>          // 包含Qt网络请求类的定义，用于构建网络请求
#include <QNetworkReply>            // 包含Qt网络回复类的定义，用于处理网络响应
#include <QUrl>                     // 包含Qt URL类的定义，用于表示和操作URL
#include <QDebug>                   // 包含Qt调试输出宏定义
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <vector>
#include <QVariant>

class HttpPostRequest : public QObject // 定义一个名为HttpPostRequest的类，继承自QObject
{
    Q_OBJECT // 使用Q_OBJECT宏，使得该类能够使用Qt的信号和槽机制

public:
    explicit HttpPostRequest(const QString &url, const QString &jwt_token, const QJsonObject &data, QObject *parent = nullptr)
        : QObject(parent),
          url(url),
          jwt_token(jwt_token),
          data(data),
          m_manager(new QNetworkAccessManager(this)),
          m_request(new QNetworkRequest(QUrl(url))) // 构造函数，初始化成员变量
    {

        QJsonDocument doc = QJsonDocument(data);
        QByteArray jsonData = doc.toJson();

        qDebug() << jsonData << endl;

        m_request->setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
        m_request->setRawHeader("Authorization", QByteArray("Bearer ").append(jwt_token.toUtf8()));
        m_reply = m_manager->post(*m_request, jsonData);

        connect(m_reply, &QNetworkReply::finished, this, &HttpPostRequest::onFinished); // 连接m_reply对象的finished()信号到onFinished()槽函数
    }

     HttpPostRequest(QObject *parent = nullptr): QObject(parent){}

signals:
     void post_finished();

public slots:
    void onFinished() // onFinished()槽函数，当请求完成时调用
    {
        QByteArray msg = m_reply->readAll();
        this->msgs = QJsonDocument::fromJson(msg);

        this->msg_code = this->msgs["code"].toDouble();
        this->msg_data = this->msgs["data"];
        this->msg_message = this->msgs["message"].toString();

        qDebug() << "请求结果如下:" << endl;
        this->print_return_info();
        qDebug() << "----------" << endl;

        m_reply->deleteLater(); // 删除m_reply对象

        emit post_finished();
    }

    void onError(QNetworkReply::NetworkError error) // onError()槽函数，当请求发生错误时调用
    {
        qDebug() << "Error:" << error; // 输出错误信息
        m_reply->deleteLater(); // 删除m_reply对象
    }

private:
    QNetworkAccessManager *m_manager;   // Qt网络访问管理器对象，用于处理网络请求
    QNetworkRequest *m_request;         // Qt网络请求对象，用于构建网络请求
    QNetworkReply *m_reply;             // Qt网络回复对象，用于处理网络响应
    QString url;
    QString jwt_token;
    QJsonObject data;


public:
//    QString baseUrl = "http://192.168.8.20:3000/api";
    QString baseUrl = "http://192.168.56.1:3000/api";

    QJsonDocument msgs;
    QJsonObject msg2obj;
    QJsonArray msg2arr;

    double msg_code;
    QJsonValue msg_data;
    QString msg_message;

    void print_return_info() {
        qDebug() << this->msg_code << endl << this->msg_data << endl << this->msg_message << endl;
    }

    double get_return_code() {
        return this->msg_code;
    }

    QString get_return_message() {
        return this->msg_message;
    }

    QJsonValue get_return_data() {
        return this->msg_data;
    }

    void post(const QString &url, const QString &jwt_token, const QJsonObject &data) {
        this->m_manager = new QNetworkAccessManager();
        QString newUrl = this->baseUrl + url;
        this->m_request = new QNetworkRequest(QUrl(newUrl));

        QJsonDocument doc = QJsonDocument(data);
        QByteArray jsonData = doc.toJson();

        qDebug() << "请求api如下:" << endl << newUrl << endl << "----------" << endl;
        qDebug() << "请求信息如下:" << endl << data << endl << "----------" << endl;

        m_request->setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
        m_request->setRawHeader("Authorization", QByteArray("Bearer ").append(jwt_token.toUtf8()));
        m_reply = m_manager->post(*m_request, jsonData);

        connect(m_reply, &QNetworkReply::finished, this, &HttpPostRequest::onFinished);
    }
};

#endif // HTTPPOSTREQUEST

